﻿Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class connection

    Private Shared IP As IPAddress
    Private Shared port As System.Net.IPEndPoint
    Private Shared socket As System.Net.Sockets.Socket

    Public Sub New(ByVal ipCode As String, ByVal portNumber As String)

        IP = IPAddress.Parse(ipCode)
        port = New System.Net.IPEndPoint(IP, portNumber)
        socket = New System.Net.Sockets.Socket(IP.AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)

        socket.Connect(port)

    End Sub

    Public Sub sendData(ByVal action As String)

        action += vbCrLf
        Dim myBuffer As Byte() = Encoding.ASCII.GetBytes(action & vbCrLf)
        socket.Send(myBuffer)

    End Sub

    Public Function receiveData() As String

        Dim data As Byte() = New Byte(1000) {}
        socket.Receive(data, 0, 1000, SocketFlags.None)
        Return Encoding.ASCII.GetString(data)

        Return ""

    End Function

End Class
